#include<string>
using namespace std;

class Solution {
public:
    int monotoneIncreasingDigits(int N) {
        if (N < 10)return N;
        string str = to_string(N);
        for (int i = 0; i < str.length()-1; ++i) {
            if (str[i + 1] >= str[i])continue;
            int index = i + 1;
            while (i > 0) {
                if (str[i - 1] < str[i]) {
                    str[i] -= 1;
                    break;
                }
                --i;
            }
            if (i == 0)
                str[i] -= 1;
            while (++i < str.length()) str[i] = '9';
        }
        return atoi(str.c_str());
    }
};